package com.taoyige.ui;

import org.apache.commons.httpclient.util.DateUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taoyige.AppConfig;
import com.taoyige.AppContext;
import com.taoyige.AppException;
import com.taoyige.R;
import com.taoyige.bean.Order;
import com.taoyige.common.UIHelper;
import com.taoyige.widget.LoadingDialog;

/**
 * 订单详情
 * @author zhuxun
 * @version 1.0
 * @date 2014-11-25 上午9:56:40
 */
public class OrderDetailActivity extends BaseActivity {
	
	private AppContext appContext;
	private Handler handler;
	private LoadingDialog loading;
	
	private ImageButton returnBack;
	private ImageButton homePage;
	private TextView userHeadText;
	private String orderId;
	private String uid;
	private LinearLayout orderDetailOperationLl;
	
	private Button operationRecepit;
	private Button operationTrack;
	
	private TextView orderNo;
	private TextView orderStatus;
	private ImageView orderPayMethod;
	private TextView orderTime;
	private ImageView orderProductImageUrl;
	private TextView orderProductTitle;
	private TextView orderProductPrice;
	private TextView orderProductCount;
	private TextView orderRecipientName;
	private TextView orderRecipientMobile;
	private TextView orderRecipientAddr;
	private TextView orderCount;
	private TextView orderTotalPrice;
	private TextView orderExpressCost;
	private TextView orderTotalMoney;
	
	private View orderOperationCancel;
	private View orderOperationDelivered;
	private View orderOperationNoPaid;
	
	private Button operationBuy;
	
	private LinearLayout orderDetailLl;
	private LinearLayout orderDetailInfoLi;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		appContext=(AppContext) getApplication();
		setContentView(R.layout.order_detail);
		
		this.initView();
		this.initData();
	}
	
	private void initView(){
		orderId = getIntent().getStringExtra("orderId");
		uid = getIntent().getStringExtra("uid");
		
		returnBack=(ImageButton)findViewById(R.id.user_hear_return_back);
		homePage=(ImageButton)findViewById(R.id.user_hear_homePage);
		userHeadText=(TextView)findViewById(R.id.user_hear_text);
		userHeadText.setText("订单详情");
		
		  orderNo=(TextView) findViewById(R.id.order_detail_id_value);
		  orderStatus=(TextView) findViewById(R.id.order_detail_orderstatus_value);
		  orderPayMethod=(ImageView) findViewById(R.id.order_pay_menthod_value);
		  orderTime=(TextView) findViewById(R.id.order_detail_ordertime_value);
		  orderProductImageUrl=(ImageView) findViewById(R.id.order_detail_pic);
		  orderProductTitle=(TextView) findViewById(R.id.order_detail_title);
		  orderProductPrice=(TextView) findViewById(R.id.order_detail_money_value);
		  orderProductCount=(TextView) findViewById(R.id.order_detail_pay_num);
		  orderRecipientName=(TextView) findViewById(R.id.order_detail_name_value);
		  orderRecipientMobile=(TextView) findViewById(R.id.order_detail_phone_value);
		  orderRecipientAddr=(TextView) findViewById(R.id.order_detail_address_value);
		  orderTotalPrice=(TextView) findViewById(R.id.order_buymoney_value);
		  orderExpressCost=(TextView) findViewById(R.id.order_freight_value);
		  orderTotalMoney=(TextView) findViewById(R.id.order_paymoney_value);
		
		  orderDetailLl=(LinearLayout) findViewById(R.id.order_detail_ll);
		  orderDetailInfoLi=(LinearLayout)findViewById(R.id.order_detail_info_ll);
		  
		orderDetailOperationLl=(LinearLayout)findViewById(R.id.order_detail_operation_ll);
		LayoutInflater inflater = getLayoutInflater();
		
		 orderOperationCancel=inflater.inflate(R.layout.order_cancel, null);
		 orderOperationDelivered=inflater.inflate(R.layout.order_edit, null);
		 orderOperationNoPaid=inflater.inflate(R.layout.order_buy, null);
		
		operationRecepit=(Button)orderOperationDelivered.findViewById(R.id.order_detail_operation_recepit);
		operationTrack=(Button)orderOperationDelivered.findViewById(R.id.order_detail_operation_track);
		
		operationBuy=(Button)orderOperationNoPaid.findViewById(R.id.order_detail_buy_button);
		
		returnBack.setOnClickListener(userListener);
		homePage.setOnClickListener(homeListener);
		
		orderDetailInfoLi.setVisibility(View.INVISIBLE);
	}
	

	private void initData() {
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (loading != null)
					loading.dismiss();
				if (msg.what == 1 && msg.obj != null) {
					Order order=(Order) msg.obj;
					
					 orderNo.setText(orderId);
					 
					  orderTime.setText(DateUtil.formatDate(order.getTimestamp(), "yyyy-MM-dd hh:mm:ss"));
					  
					  UIHelper.showGoodsImage(orderProductImageUrl, "http://192.168.1.200:3001"+order.getProduct().getThumbImageUrl().get(0));
					  orderProductTitle.setText(order.getProduct().getTitle());
					  orderProductPrice.setText(order.getProduct().getPrice()+"");
					  orderProductCount.setText("数量：1");
					  orderRecipientName.setText(order.getAddressInfo().getName());
					  orderRecipientMobile.setText(order.getAddressInfo().getMobilephone());
					  
					  orderRecipientAddr.setText(order.getAddressInfo().getDetails());
					  orderTotalPrice.setText(order.getProduct().getPrice()+"");
					  orderExpressCost.setText("0");
					  orderTotalMoney.setText(order.getTotalPrice()+"");
					  
					  //orderStatus.setText(order.getStatus())
					 // orderPayMethod.setText(order.getPaymentPlatform());
					  
					  switch(order.getPaymentPlatform()){
						case AppConfig.PAY_PLATFFROM_ALIPAY:
							orderPayMethod.setImageResource(R.drawable.zhifubao);
							break;
						case AppConfig.PAY_PLATFFROM_WECHAT:
							orderPayMethod.setImageResource(R.drawable.zhifubao);
							break;
						
						}
					  
					  RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
						//lp.setMargins(0,0,0,0);
						
						switch(order.getStatus()){
						case AppConfig.ORDER_UNPAID:
							orderStatus.setText(getString(R.string.orde_status_unpaid));
							//添加立即付款按钮
							orderDetailOperationLl.addView(orderOperationNoPaid);
							orderOperationNoPaid.setTag(order);
							orderOperationNoPaid.setOnClickListener(gotoPayListener);
							break;
						case AppConfig.ORDER_PAID:
							orderStatus.setText(getString(R.string.orde_status_paid));
							operationBuy.setText(getString(R.string.order_cancel));
							orderDetailOperationLl.addView(orderOperationNoPaid);
							orderOperationNoPaid.setTag(order);
							orderOperationNoPaid.setOnClickListener(cancelListener);
							break;
						case AppConfig.ORDER_DELIVERED:
							orderStatus.setText(getString(R.string.orde_status_delivered));
							orderDetailOperationLl.addView(orderOperationDelivered);
							operationRecepit.setOnClickListener(recepitListener);
							operationTrack.setTag(order);
							operationTrack.setOnClickListener(trackListener);
							break;
						case AppConfig.ORDER_CHECKED:
							orderStatus.setText(getString(R.string.orde_status_checked));
							operationBuy.setText(getString(R.string.orde_return_goods));
							orderDetailOperationLl.addView(orderOperationNoPaid);
							orderOperationNoPaid.setTag(order);
							orderOperationNoPaid.setOnClickListener(returnGoodsListener);
							break;
						case AppConfig.ORDER_CLOSED:
							orderDetailLl.setLayoutParams(lp);
							orderStatus.setText(getString(R.string.orde_status_closed));
							break;
						case AppConfig.ORDER_DELETED:
							orderDetailLl.setLayoutParams(lp);
							orderStatus.setText(getString(R.string.orde_status_deleted));
							break;
						case AppConfig.ORDER_REFUND:
							orderDetailLl.setLayoutParams(lp);
							orderStatus.setText(getString(R.string.orde_status_refund));
							break;
						case AppConfig.ORDER_REFUND_SUCCESS:
							orderDetailLl.setLayoutParams(lp);
							orderStatus.setText(getString(R.string.orde_status_refund_success));
							break;
						case AppConfig.ORDER_RETURN_GOODS:
							orderDetailLl.setLayoutParams(lp);
							orderStatus.setText(getString(R.string.orde_status_return_goods));
							break;
						case AppConfig.ORDER_RETURN_GOODS_SUCCESS:
							orderDetailLl.setLayoutParams(lp);
							orderStatus.setText(getString(R.string.orde_status_return_goods_scueess));
							break;
						}
					  
						orderDetailInfoLi.setVisibility(View.VISIBLE);
				} else if (msg.obj != null) {
					((AppException) msg.obj).makeToast(OrderDetailActivity.this);
				}
				
			}
		};
		
		loading = new LoadingDialog(this);
		loading.show();
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					Order  order= appContext.getOrderDetail(orderId, uid);
					msg.what = 1;
					msg.obj = order;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();

	}

	
	public View.OnClickListener returnGoodsListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			UIHelper.ToastMessage(v.getContext(), v.getContext().getString(R.string.user_return_goods_refund));
		}
	};
	
	public View.OnClickListener cancelListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			UIHelper.showHomePage(v.getContext());
		}
	};
	
	
	public View.OnClickListener gotoPayListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				UIHelper.showHomePage(v.getContext());
			}
		};
	
		public View.OnClickListener recepitListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//更新订单信息，弹出提示 TODO
				UIHelper.ToastMessage(v.getContext(), v.getContext().getString(R.string.user_recepit));
			}
		};
		public View.OnClickListener trackListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				UIHelper.showOrderTrack(v.getContext(), ((Order)v.getTag()).getId(), ((Order)v.getTag()).getUserID());
			}
		};
		
	public View.OnClickListener homeListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			UIHelper.showHomePage(v.getContext());
		}
	};
	
	public View.OnClickListener userListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			UIHelper.showUser(v.getContext());
		}
	};
	public View.OnClickListener orderStatusListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			UIHelper.showOrderStatus(v.getContext(),((Order)v.getTag()).getId(),((Order)v.getTag()).getUserID());
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
